package com.suke.czx.modules.projectType.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.*;
import com.suke.czx.common.validate.ExpressUpdateGroup;
import com.suke.czx.common.validate.ProductSaveGroup;
import com.suke.czx.modules.projectType.entity.ProjectType;
import com.suke.czx.modules.projectType.service.ProjectTypeService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 应用模块名称: 项目类型
 * 代码描述: 产品控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/06 18:06:39
 */
@Validated
@RestController
@RequestMapping("/api/projectType")
@AllArgsConstructor
@Api(value = "ProjectTypeController", tags = "项目类型管理")
public class ProjectTypeController extends AbstractController {

    private final ProjectTypeService projectTypeService;

    /**
     * 分页查询项目类型列表
     * @param params 查询参数
     * @return VO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:projectType:list')")
    public R list(@RequestParam Map<String, Object> params) {
        if (StrUtil.isBlankIfStr(params.get(FieldConstant.PAGE)) || StrUtil.isBlankIfStr(FieldConstant.LIMIT)) {
            return R.error("缺少分页信息");
        }
        // 查询列表数据
        QueryWrapper<ProjectType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(FieldConstant.DEL_FLG, CodeConstant.NOT_DEL);
        if (params.size() > 2){
            queryWrapper.like(!StrUtil.isBlankIfStr(params.get(FieldConstant.PROJECT_TYPE)),
                    StrUtil.toUnderlineCase(FieldConstant.PROJECT_TYPE), params.get(FieldConstant.PROJECT_TYPE));
        }
        IPage<ProjectType> expressPage = projectTypeService.page(mpPageConvert.pageParamConvert(params), queryWrapper);
        return Objects.requireNonNull(R.ok().put(FieldConstant.TOTAL, expressPage.getTotal())).setData(Optional.ofNullable(expressPage.getRecords()).orElse(Lists.newArrayList()));
    }

    /**
     * 查询所有项目类型
     * @param productIds 产品ids
     * @return VO
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:projectType:select')")
    public R select(String productIds) {
        return R.ok().setData(Optional.ofNullable(projectTypeService.selectProjectType(productIds)).orElse(Lists.newArrayList()));
    }

    /**
     * 获取项目类型详情
     * @param projectTypeId 项目类型id
     * @return VO
     */
    @RequestMapping(value = "/info/{projectTypeId}", method = RequestMethod.GET)
    @PreAuthorize("hasRole('but:projectType:info')")
    public R info(@PathVariable("projectTypeId") Long projectTypeId) {
        ProjectType projectType = projectTypeService.getById(projectTypeId);
        return R.ok().setData(projectType);
    }

    /**
     * 保存项目类型
     * @param projectType 项目类型
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存项目类型")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:projectType:save')")
    public R save(@Validated(ProductSaveGroup.class) ProjectType projectType, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        projectType.setCreator(getUserId());
        projectType.setCreateTime(new Date());
        projectTypeService.save(projectType);
        return R.ok();
    }

    /**
     * 更新项目类型
     * @param projectType 项目类型
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新项目类型")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:projectType:update')")
    public R update(@Validated(ExpressUpdateGroup.class) ProjectType projectType, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        projectType.setUpdater(getUserId());
        projectType.setUpdateTime(new Date());
        projectTypeService.updateById(projectType);
        return R.ok();
    }

    /**
     * 删除项目类型
     * @param projectTypeId 项目类型id
     * @return VO
     */
    @SysLog("删除产品")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:projectType:delete')")
    public R delete(@NotNull(message = "产品编码不能为空") Long projectTypeId) {
        return projectTypeService.updateDelProjectType(projectTypeId, getUserId());
    }

}